package com.water.app.Controller.account;

import com.water.app.*;
import com.water.app.Impl.account.EmailServiceImpl;
import com.water.app.interfaces.account.userService;
import jakarta.annotation.Resource;
import opennlp.tools.parser.Cons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.water.app.Impl.account.LoginServiceImpl;

import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/api")
public class LoginController {

    private static final Logger log= LoggerFactory.getLogger(LoginController.class);
    @Autowired
    LoginServiceImpl loginService;
    @Autowired
    EmailServiceImpl emailService;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    private userService userService;


    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if (user == null) {
            log.error("用户操作错误!");
            return Result.error("用户操作错误!");
        }
        log.info("user:{}", user);
        if (user.getAccount() == null || user.getPassword() == null) {
            log.error("用户名或密码不能为空!");
            return Result.error("用户名或密码不能为空!");
        }

        User user1 = loginService.SelectUser(user.getAccount(), user.getPassword());

        if (user1 == null) {
            log.error("用户名或密码错误!");
            return Result.error("用户名或密码错误!");
        }
        log.info("user1:{}",user1);
        int id = user1.getId();
        LoginInfo loginInfo=null;
        String token=null;
        if(user1.getIsManager()==0){
            loginInfo= LoginInfo.builder().setIsManager(0)
                    .setId(id)
                    .setUserName(user1.getUsername())
                    .build();
            token = JwtUtil.generateJwt(id);
            stringRedisTemplate.opsForValue().set(Constant.LOGIN_USER + id, token);
        }else if(user1.getIsManager()==1){
            loginInfo=LoginInfo.builder().setIsManager(1)
                    .setId(id)
                    .setUserName(user1.getUsername())
                    .build();
            token = JwtUtil.generateJwt(id);
            stringRedisTemplate.opsForValue().set(Constant.LOGIN_MANAGER + id, token);
        }else{
            return Result.error("你的账号已经被封禁，请联系管理员了解详情");
        }
        loginInfo.setToken(token);
        return Result.success(loginInfo);
    }

    @PostMapping("/selectUser")
    public Result selectUser(){
        log.info("正在查询用户信息");
        Integer userId = Constant.THREAD_LOCAL.get();
        User user=null;
        if(userId!=null) {
            user = loginService.selectUserById(userId);
        }
        if(user!=null) {
            return Result.success(user);
        }else{
            log.info("查询用户信息失败");
            return Result.error("查询用户信息失败");
        }
    }

    @PostMapping("/deleteUser")
    public Result deleteUser(){
        log.info("正在退出登录");
        Integer userId = Constant.THREAD_LOCAL.get();
        log.info("userId:{}",userId);
        Boolean result = stringRedisTemplate.delete(Constant.LOGIN_USER + userId);
        log.info("userId:{}",userId);
        if(Boolean.TRUE.equals(result)){
            log.info("退出登录成功");
            return Result.success("退出登录成功");
        }else {
            log.info("退出登录失败");
            return Result.error("退出登录失败");
        }

    }

    @PostMapping("/isManager")
    public Result isManager(@RequestHeader("Authorization") String authToken){

        Integer userId = JwtUtil.parseJwt(authToken);
        int status= userService.selectIsManager(userId);
        return Result.success(status);
    }

}
